// Compile with gcc -std=c98 -pthread lab7a.c -o lab7a

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define ARRAY_SIZE	500
#define NUM_THREADS    10 

long longArray[500];

void *PrintHello(void *threadid);

void *PrintHello(void *threadid)
{	
	// TID 0 updates 0,1,2,3,4
	// TID 1 updates 5,6,7,8,9
	// TID 2 updates 10, 11, 12, 13, 14
	// tid x 5
	int block = (ARRAY_SIZE)/NUM_THREADS;
	//printf("block:%d", block);
	//int startIndex = (int)threadid * 5;
        //for(int j=startIndex; j<startIndex+5; j++)
	int startIndex = (int)threadid * block;
	for(int j=startIndex; j<startIndex+block; j++)
	{	
		longArray[j] = (long)threadid;
	}
}

int main (int argc, char *argv[])
{
   	pthread_t threads[NUM_THREADS];

   	for(int t = 0; t < NUM_THREADS; t++)
	{
		printf("In main(): creating thread %d \n",t);
	   	int rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
   	}

	for(int t = 0; t < NUM_THREADS; t++)
	{
		pthread_join(threads[t], NULL);
	}

        for(int i=0;i<ARRAY_SIZE-1;i++)
        {   
                printf("%ld\n",longArray[i]);
        }   


   	/* Last thing that main() should do */
   	pthread_exit(NULL);
}

// void* => can be cast to any data type
